package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.lang.Result;
import com.example.entity.Company;
import com.example.entity.Recruit;
import com.example.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyk
 * @since 2022-02-19
 */
@RestController
public class CompanyController {

    @Autowired
    CompanyService companyService;

    /**
     * 查询全部企业用户信息，分页显示
     * @param currentPage
     * @return
     */
    @GetMapping("/companyAll")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage){

        Page page = new Page(currentPage, 5);
        QueryWrapper queryWrapper = new QueryWrapper<Company>();
        IPage pageData = companyService.page(page, queryWrapper);

        return Result.succ(pageData);
    }

    /**
     * 根据用户id获取用户的信息
     * @param id
     * @return
     */
    @GetMapping("/company/{user_id}")
    public Result companyById(@PathVariable(name = "user_id") Long id){

        QueryWrapper queryWrapper = new QueryWrapper<Recruit>().eq("user_id",id);
        Company company = companyService.getOne(queryWrapper);

        return Result.succ(company);
    }

    @GetMapping("/saveCompany/{user_id}")
    public Result save(@PathVariable(name = "user_id") Long id){

        int i = companyService.saveCompany(id);
        if (i > 0){
            return Result.succ(200,"操作成功！",null);
        } else {
            return Result.succ(200,"操作失败！",null);
        }

    }

    @PostMapping("/companyEdit")
    public Result edit(@Validated @RequestBody Company company){

        Company temp = new Company();
        temp.setId(company.getId());
        temp.setUserId(company.getUserId());
        temp.setName(company.getName());
        temp.setSex(company.getSex());
        temp.setPhone(company.getPhone());
        temp.setEmail(company.getEmail());
        temp.setCname(company.getCname());
        temp.setAddress(company.getAddress());

        boolean i = companyService.updateCompany(temp);

        return Result.succ(null);
    }

}
